package com.macaline.framemarker.dataBase.mysql.util;

import cn.hutool.core.util.StrUtil;
import com.macaline.framemarker.dataBase.framework.bean.Column;
import com.macaline.framemarker.dataBase.framework.bean.ColumnList;
import com.macaline.framemarker.dataBase.framework.bean.Const;
import com.macaline.framemarker.dataBase.framework.bean.Table;
import com.macaline.framemarker.dataBase.mysql.enums.ColumnType;
import com.macaline.framemarker.dataBase.mysql.exception.TableConfigException;

import java.util.List;

/**
 * sql语句工具类
 */
public class SqlUtil {

    public static void main(String[] args) throws Exception {
        Table table = new Table();
        table.setName("table");
        Column column = Column.builder().build();
        column.setName("id");
        column.setPrimaryKey(1);
        column.setAutoAdd(1);
        column.setNotNull(1);
        column.setLength(11);
        column.setType(ColumnType.INT.getCode());
        column.setComment("id");
        table.addColumn(column);
        Column column1 = Column.builder().build();
        column1.setName("name");
        column1.setType(ColumnType.CHAR.getCode());
        column1.setLength(20);
        column1.setComment("姓名");
        table.addColumn(column1);
        String insertSql = getInsertSql(table);
        System.out.println(insertSql);
    }


    public static String getInsertSql(Table table) throws TableConfigException {
        ColumnList<String> primaryKeyList = new ColumnList<String>();
        StringBuffer buffer = new StringBuffer();
        buffer.append("CREATE TABLE " + table.getName() + "(\n");
        List<Column> columns = table.getColumns();
        for(Column item : columns){
            // 字段名
            buffer.append(item.getName() + " ");
            // 字段类型、长度
            buffer.append(ColumnType.getTypeNameByCode(item.getType()) + "(" + item.getLength() + ") ");
            // 是否可为空
            if(item.getNotNull() != null && item.getNotNull() - Const.IS_YES == 0){
                buffer.append("NOT NULL ");
            }

            // 是否为主键,暂不支持多主键
            if(item.getPrimaryKey() - Const.IS_YES == 0){
                primaryKeyList.add(item.getName());
                buffer.append("PRIMARY KEY ");
            }
            // 自增长
            if(item.getAutoAdd() - Const.IS_YES == 0){
                buffer.append("AUTO_INCREMENT ");
            }
            // 默认值
            String defaultValue = item.getDefaultValue();
            if(defaultValue != null){
                buffer.append("DEFAULT " + defaultValue);
            }
            // 注释
            String comment = item.getComment();
            if(comment != null){
                buffer.append("comment '" + comment + "'");
            }
            buffer.append(",\n");
        }
        // 去掉最后的那个逗号
        buffer.setLength(buffer.length() - 2);
        buffer.append("\n);\n");
        // 处理主键信息
        if(primaryKeyList.size() > 1){
            throw new TableConfigException("一张表不允许多个主键存在。");
//            buffer.append("ALTER TABLE " + table.getName() + " ADD PRIMARY KEY(" + primaryKeyList.getColumnsSql() + ");");
        }
        return buffer.toString();
    }

    /**
     * 获取java
     */
    public static String getBeanCode(Table table) {
        StringBuilder stringBuilder = new StringBuilder();
        table.getColumns().forEach(column -> {
            stringBuilder.append("/**\n" +
                    " * " + column.getComment() + "\n" +
                    " */\n");
            stringBuilder.append("@Column(name = \"" + column.getName() + "\")\n");
            stringBuilder.append("private String " + StrUtil.toCamelCase(column.getName()) + ";\n");
        });
        return stringBuilder.toString();
    }

    public static String getDropTable(String tableName) {
        return "DROP TABLE " + tableName + ";";
    }
}
